package com.atwisdom.star.controller;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.basicClass.impl.MeshLibBslServiceImpl;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.atwisdom.star.log.aop.StarAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/mesh")
public class MeshLibController {

    private MeshLibBslServiceImpl meshLibBslService;
    @Autowired
    public void setMeshLibBslService(MeshLibBslServiceImpl meshLibBslService) {
        this.meshLibBslService = meshLibBslService;
    }


    /**
     *
     * @param actionDataList 接收参数据
     * @return 返回结果
     * @throws BusinessException  业务异常
     */
    @StarAnnotation
    @PostMapping
    public  ResultInfo<?> responseMeshFactory(@RequestBody List<ActionData<JSONObject>> actionDataList) throws BusinessException {
          return meshLibBslService.responseFactory(actionDataList);
    }

    @StarAnnotation
    @PostMapping(value = "/downloadFile")
    public void meshDownloadFactory(@RequestBody List<ActionData<JSONObject>> actionDataList, HttpServletResponse response, HttpServletRequest request) throws BusinessException {
        ActionData<JSONObject> actionData = actionDataList.get(0);
        String action = actionData.getAction();
        if (action.equalsIgnoreCase("MESH_DOWNLOAD_FILE_BY_NAME")) {
            this.meshLibBslService.downloadFileByName(actionData, response, request);
        }

        if (action.equalsIgnoreCase("MESH_DOWNLOAD_FILE_BY_ID")) {
            this.meshLibBslService.downloadFileById(actionData, response, request);
        } else {
            throw new BusinessException("999999", "action编码类型不匹配，下载文件失败！");
        }
    }

    /**
     *
     * @param file 接收参数据
     * @return 返回结果
     * @throws BusinessException  异常
     */
    @StarAnnotation
    @PostMapping(value = "/uploadFile")
    public ResultInfo<?> uploadFile(@RequestParam MultipartFile file, @RequestParam Long id) throws BusinessException {
        return meshLibBslService.uploadFile(file, id);
    }
}
